package io.renren.modules.aic.controller;

import io.renren.common.annotation.SysLog;
import io.renren.common.utils.DateUtils;
import io.renren.common.utils.DecimalUtils;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.ProjectConstant;
import io.renren.common.utils.R;
import io.renren.common.validator.Assert;
import io.renren.modules.aic.entity.CtocOrderEntity;
import io.renren.modules.aic.entity.MemberEntity;
import io.renren.modules.aic.service.CtocOrderService;
import io.renren.modules.aic.service.MemberService;

import java.util.Date;
import java.util.Map;

import org.apache.commons.lang.RandomStringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;

/**
 * ctoc订单表
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2018-03-30 15:44:17
 */
@RestController
@RequestMapping("aic/ctocorder")
public class CtocOrderController {
	@Autowired
	private CtocOrderService ctocOrderService;

	@Autowired
	private MemberService memberService;

	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("aic:ctocorder:list")
	public R list(@RequestParam Map<String, Object> params) {
		PageUtils page = ctocOrderService.queryPage(params);
		return R.ok().put("page", page);
	}

	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("aic:ctocorder:info")
	public R info(@PathVariable("id") Long id) {

		CtocOrderEntity ctocOrder = ctocOrderService.selectById(id);
		MemberEntity member = memberService.selectById(ctocOrder.getMemberId());
		ctocOrder.setOrderNo(member.getMobile());

		return R.ok().put("ctocOrder", ctocOrder);
	}

	/**
	 * 保存
	 */
	@SysLog("新增买单")
	@RequestMapping("/save")
	@RequiresPermissions("aic:ctocorder:save")
	public R save(@RequestBody CtocOrderEntity ctocOrder) {
		Assert.hasText(ctocOrder.getMobile(), "手机号必须填写");
		Assert.notNull(ctocOrder.getCoinPrice(), "币价格不能为空");
		Assert.notNull(ctocOrder.getCoinQuantity(), "币数量不能为空");
		Assert.isTrue(ctocOrder.getCoinPrice() > 0, "币价必须大于0");
		Assert.isTrue(ctocOrder.getCoinQuantity() > 0, "币数量必须大于0");
		MemberEntity member = memberService.selectOne(new EntityWrapper<MemberEntity>().eq("mobile", ctocOrder.getMobile()));
		Assert.notNull(member, "手机号不存在");

		ctocOrder.setMemberId(member.getId());
		ctocOrder.setOrderNo("C" + DateUtils.format(new Date(), "yyyyMMddHH") + RandomStringUtils.randomNumeric(5));
		ctocOrder.setCoinTotalPrice(DecimalUtils.mul(ctocOrder.getCoinPrice(), ctocOrder.getCoinQuantity()));
		ctocOrder.setCreateTime(new Date());
		ctocOrderService.insert(ctocOrder);

		return R.ok();
	}

	/**
	 * 修改
	 */
	@SysLog("修改订单")
	@RequestMapping("/update")
	@RequiresPermissions("aic:ctocorder:update")
	public R update(@RequestBody CtocOrderEntity ctocOrder) {
		Assert.notNull(ctocOrder.getCoinPrice(), "币数价格不能为空");
		Assert.notNull(ctocOrder.getCoinQuantity(), "币数量不能为空");
		Assert.isTrue(ctocOrder.getCoinPrice() > 0, "币数必须大于0");
		Assert.isTrue(ctocOrder.getCoinQuantity() > 0, "币数量必须大于0");

		ctocOrder.setCoinTotalPrice(DecimalUtils.mul(ctocOrder.getCoinPrice(), ctocOrder.getCoinQuantity()));
		ctocOrder.setOrderNo(null);
		ctocOrder.setMemberId(null);
		ctocOrder.setUpdateTime(new Date());
		ctocOrderService.updateById(ctocOrder);

		return R.ok();
	}

	/**
	 * 取消挂单
	 */
	@SysLog("取消挂单")
	@RequestMapping("/cancelOrder")
	@RequiresPermissions("aic:ctocorder:update")
	public R cancelOrder(@RequestBody Long[] ids) {
		Assert.notNull(ids, "参数不能为空");
		Assert.noNullElements(ids, "参数数据无效");
		CtocOrderEntity entity = new CtocOrderEntity();
		entity.setStatus(ProjectConstant.CTOC_ORDER_STATUS_CANCEL);
		entity.setUpdateTime(new Date());
		ctocOrderService.update(entity, new EntityWrapper<CtocOrderEntity>().in("id", ids));

		return R.ok();
	}

}
